package com.flyco.modules.stm.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.modules.pms.entity.PmsSkuStock;
import com.flyco.modules.pms.service.IPmsSkuStockService;
import com.flyco.modules.stm.entity.StmChannelProduct;
import com.flyco.modules.stm.service.IStmChannelPriceService;
import com.flyco.modules.stm.service.IStmChannelProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 渠道产品表
 * @Author: flyco
 * @Date: 2023-06-21
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "渠道产品表")
@RestController
@RequestMapping("/f/stm/stmChannelProduct")
public class StmChannelProductController extends BaseController<StmChannelProduct, IStmChannelProductService> {
    @Autowired
    private IStmChannelProductService stmChannelProductService;
    @Autowired
    private IPmsSkuStockService skuStockService;
    @Autowired
    private IStmChannelPriceService stmChannelProductJdService;


    /**
     * 分页列表查询
     *
     * @param stmChannelProduct
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "渠道产品表-分页列表查询")
    @ApiOperation(value = "渠道产品表-分页列表查询", notes = "渠道产品表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StmChannelProduct stmChannelProduct,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        IPage<StmChannelProduct> pageList = new Page<>();
        if (stmChannelProduct == null || stmChannelProduct.getChannelId() == null) {
            return Result.ok(pageList);
        }
        QueryWrapper<StmChannelProduct> queryWrapper = QueryGenerator.initQueryWrapper(stmChannelProduct, req.getParameterMap());
        queryWrapper.orderByDesc("id");
        Page<StmChannelProduct> page = new Page<StmChannelProduct>(pageNo, pageSize);
        pageList = stmChannelProductService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param stmChannelProduct
     * @return
     */
    @AutoLog(value = "渠道产品表-添加")
    @ApiOperation(value = "渠道产品表-添加", notes = "渠道产品表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StmChannelProduct stmChannelProduct) {
        stmChannelProductService.save(stmChannelProduct);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param stmChannelProduct
     * @return
     */
    @AutoLog(value = "渠道产品表-编辑")
    @ApiOperation(value = "渠道产品表-编辑", notes = "渠道产品表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StmChannelProduct stmChannelProduct) {
        stmChannelProductService.updateById(stmChannelProduct);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "渠道产品表-通过id删除")
    @ApiOperation(value = "渠道产品表-通过id删除", notes = "渠道产品表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {

        StmChannelProduct channelProduct = stmChannelProductService.getById(id);
        stmChannelProductService.removeById(id);

        //同时sku表的记录标记为删除
        if(channelProduct.getStatus().equals(1)){
            skuStockService.updateCancelFlag(channelProduct.getProductSn(),channelProduct.getChannelId(),1);
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "渠道产品表-批量删除")
    @ApiOperation(value = "渠道产品表-批量删除", notes = "渠道产品表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.stmChannelProductService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "渠道产品表-通过id查询")
    @ApiOperation(value = "渠道产品表-通过id查询", notes = "渠道产品表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StmChannelProduct stmChannelProduct = stmChannelProductService.getById(id);
        return Result.ok(stmChannelProduct);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param stmChannelProduct
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StmChannelProduct stmChannelProduct) {
        return super.exportXls(request, stmChannelProduct, StmChannelProduct.class, "渠道产品表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StmChannelProduct.class);
    }


    @AutoLog(value = "渠道产品表-定时设置产品价格是否生效")
    @ApiOperation(value = "渠道产品表-定时设置产品价格是否生效", notes = "渠道产品表-定时设置产品价格是否生效")
    @GetMapping(value = "/setChannelProductStatus")
    public Result<?> setChannelProductStatus() {
        String msg = stmChannelProductJdService.setChannelPrice();
        return Result.ok("执行结果："+msg);
    }

}
